﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class Customer : BasePage
    {
        static ICustomerBLL custBLL = (ICustomerBLL)IocContainer.Instance.Resolve<ICustomerBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!Page.IsPostBack)
            {
                // 按钮权限控制
                ControlAuthority(new List<WebControl> { btnAdd, btnUpdate, btnDelete }, 18);
                //BindData();
            }
        }       

        [WebMethod]
        public static Boolean Delete(int id)
        {

            Boolean result = false;
            try
            {
                custBLL.Delete(id);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
        }

        //private void BindData()
        //{
        //    try
        //    {
        //        StringBuilder strs = new StringBuilder();
        //        string strWhere = "";
        //        if (this.ddlstCustLevel.SelectedValue != "0")
        //            strWhere += string.Format(" and fjb ={0}", ddlstCustLevel.SelectedValue);
        //        if (!string.IsNullOrEmpty(this.txtCustNo.Text.Trim()))
        //            strWhere += string.Format(" and FNumber like '%{0}%'", this.txtCustNo.Text.Trim());
        //        if (!string.IsNullOrEmpty(this.txtCustName.Text.Trim()))
        //            strWhere += string.Format(" and FName like '%{0}%'", this.txtCustName.Text.Trim());
        //        DataTable dt = custBLL.Query(strWhere);
        //        if (dt != null && dt.Rows.Count > 0)
        //        {
        //            foreach (DataRow dataRow in dt.Rows)
        //            {
        //                strs.AppendFormat("<tr rel='{0}'>", dataRow["FItemId"]);
        //                //客户编码,客户名称,客户级别,区号,电话,手机,业务员,客户地址
        //                strs.AppendFormat("<td><a href='javascript:void(0)' rel='CustomerDetail.aspx?CustId={0}'>{1}</a></td>", dataRow["FItemId"], dataRow["FNumber"]);
        //                strs.AppendFormat("<td><a href='javascript:void(0)' rel='CustomerAddress.aspx?CustId={0}'>{1}</a></td>", dataRow["FItemId"], dataRow["FName"]);
        //                strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>",dataRow["LevelName"],
        //                    dataRow["FQH"], dataRow["FTel"], dataRow["FMobil"], dataRow["EmpName"], dataRow["FDree"]);
        //            }
        //        }
        //        litUserList.Text = strs.ToString();
        //    }
        //    catch (Exception exception)
        //    {
        //        Logger.Instance.Error(exception);
        //    }
        //}

      
        [WebMethod]
        public static string GetCustomers(string custCode, string custLevel, string custName, string orderField, string orderValue, int pageIndex, int pagePerSize)
        {
            string strResult;
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");

            if (!string.IsNullOrEmpty(custCode))
            {
                strWhere.Append(" and FNumber like '%" + custCode + "%'");
            }

            if (!string.IsNullOrEmpty(custLevel) && custLevel != "0")
            {
                strWhere.Append(" and fjb =" + custLevel);
            }
            if (!string.IsNullOrEmpty(custName))
            {
                strWhere.Append(" and FName  like '%" + custName + "%'");
            }
            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  FNumber  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  asc ");

            int totalRecords = 0;
            int totalPages = 0;
            DataTable dtProducts = custBLL.GetCustomerssByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);

            strResult = SerializationHelper.DataTable2Json(dtProducts);

            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";


        }


        //protected void btnSearch_Click(object sender, EventArgs e)
        //{
        //    BindData();
        //}

        //protected void btnhidDelete_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        int Id = 0;
        //        int.TryParse(this.selectedID.Value.Trim(), out Id);
        //        custBLL.Delete(Id);
        //        BindData();
        //    }
        //    catch (Exception exception)
        //    {
        //        Logger.Instance.Error(exception);
        //    }

        //}
    }
}